package com.mygdx.game.Sprite;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Array;
import com.mygdx.game.Scenes.ActiveCharacter;
import com.mygdx.game.Scenes.Hud;

public class NPC extends Sprite {
    public static float statetime = 0;
    public World world;
    public Body b2Body;
    TextureRegion[] walkFrame;
    TextureRegion currentFrame;
    public static Animation aniRight;
    public static float width;
    public static float heigth;

    public Stage stage;
    public ActiveCharacter activeCharacter;
    private int x;
    private int y;
    private int Radius;

    public NPC(World world, int Radius, int x, int y) {

        this.world = world;
        this.x = x;
        this.y = y;
        this.Radius = Radius;


//        texture =new Texture("Character/GreenNPC (1).png");
       // marioStand = new TextureRegion(texture);
        defineMario();
        setBounds(0, 0, 30, 30);


    }
    public void update(float dt) {
        //这里写人物的图切换
        Texture[] temp = new Texture[4];
        walkFrame = new TextureRegion[4];
        for (int i = 0; i < 4; i++) {
            temp[i] = new Texture(Gdx.files.internal("Character/GreenNPC" + " (" + (4 - i) + ").png"));
            width = temp[i].getWidth();
            heigth = temp[i].getHeight();
            walkFrame[i] = new TextureRegion(temp[i], 0, 0, 50, 50);
        }
        aniRight = new Animation(0.2f, walkFrame);
        statetime += Gdx.graphics.getDeltaTime();
        currentFrame = (TextureRegion) aniRight.getKeyFrame(statetime,true);
        setPosition(b2Body.getPosition().x - getWidth() / 2, b2Body.getPosition().y - getHeight() / 2);
        setRegion(currentFrame);
//        System.out.println(this.b2Body.getPosition());

//        if (statetime%2==0){
//            this.b2Body.applyLinearImpulse(new Vector2(40, 0f), this.b2Body.getWorldCenter(), true);
//
//        }
//        if (!(statetime%2==0)){
//            this.b2Body.applyLinearImpulse(new Vector2(40, 0f), this.b2Body.getWorldCenter(), true);
//            this.b2Body.applyLinearImpulse(new Vector2(-20, 0f), this.b2Body.getWorldCenter(), true);
//        }


    }
    public void defineMario() {
        BodyDef bdef = new BodyDef();
        bdef.position.set(x, y);
        bdef.type = BodyDef.BodyType.DynamicBody;
        b2Body = world.createBody(bdef);
        FixtureDef fdef = new FixtureDef();
        CircleShape shape = new CircleShape();
        shape.setRadius(Radius);
        fdef.shape = shape;
        fdef.restitution = 1;
        b2Body.createFixture(fdef);
    }

}

